home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / scripts / audio / mu2lin.m < prev    next >
Text File  |  1996-07-15  |  2KB  |  74 lines

  1. ## Copyright (C) 1996 John W. Eaton
  2. ##
  3. ## This file is part of Octave.
  4. ##
  5. ## Octave is free software; you can redistribute it and/or modify it
  6. ## under the terms of the GNU General Public License as published by
  7. ## the Free Software Foundation; either version 2, or (at your option)
  8. ## any later version.
  9. ##
  10. ## Octave is distributed in the hope that it will be useful, but
  11. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. ## General Public License for more details.
  14. ##
  15. ## You should have received a copy of the GNU General Public License
  16. ## along with Octave; see the file COPYING.  If not, write to the Free
  17. ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  18. ## 02111-1307, USA.
  19.  
  20. ## usage: y = mu2lin (x [, bit])
  21. ##
  22. ## If x is a vector of audio data with mu-law encoding, mu2lin (x)
  23. ## holds the same data with linear encoding.
  24. ## The optional argument bit specifies whether the input data is
  25. ## 8 bit (default) or 16 bit.
  26.  
  27. ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at>
  28. ## Created: 18 October 1994
  29. ## Adapted-By: jwe
  30.  
  31. function y = mu2lin (x, bit)
  32.  
  33.   if (nargin == 1)
  34.     bit = 8;
  35.   elseif (nargin == 2)
  36.     if (bit != 8 && bit != 16)
  37.       error ("mu2lin: bit must be either 8 or 16");
  38.     endif
  39.   else
  40.     usage ("y = mu2lin (x [, bit])");
  41.   endif
  42.  
  43.   if (! is_vector (x))
  44.     error ("mu2lin: x must be a vector");
  45.   endif
  46.  
  47.   exp_lut = [0; 132; 396; 924; 1980; 4092; 8316; 16764];
  48.  
  49.   ## invert x bitwise
  50.   x = 255 - x;
  51.  
  52.   ## determine sign of y
  53.   sig = (x > 127);
  54.  
  55.   ## determine exponent and fraction of y
  56.   e = fix (x / 16) - 8 .* sig + 1;
  57.   f = rem (x, 16);
  58.  
  59.   sig = 1 - 2 .* sig;
  60.   y = (pow2 (f, e + 2) + exp_lut (e)) .* sig;
  61.  
  62.   ## convert to 8-bit
  63.   if (bit == 8)
  64.     ld = max (abs (y));
  65.     if (ld < 16384)
  66.       sc = 64 / ld;
  67.     else
  68.       sc = 1 / 256;
  69.     endif
  70.     y = fix (y * sc);
  71.   endif
  72.  
  73. endfunction
  74.